/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavalib;

import edu.pdx.cs.guavadata.AppData;
import edu.pdx.cs.guavadata.Guava;
import edu.pdx.cs.guavadata.domain.Domain;
import edu.pdx.cs.guavadata.domain.Enumerated;
import edu.pdx.cs.guavadata.subclass.ControlType;
import edu.pdx.cs.guavadata.subclass.DataTable;
import edu.pdx.cs.guavadata.subclass.LaunchType;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

/**
 *
 * @author VuTran
 */
public class GRadioPanel extends JPanel implements Guava{
    String gname = "RadioPanel";

    public GRadioPanel() {
        super();
        setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED), "Title"));
    }

    public LaunchType getLType() {
        return LaunchType.NA;
    }

    public ControlType getCType() {
        return ControlType.Attribute;
    }

    public String getGText() {
        return ((TitledBorder) this.getBorder()).getTitle();
    }

    public void setGText(String title) {
        ((TitledBorder) this.getBorder()).setTitle(title);
    }

    public String getGName() {
        if (gname == null)
            return this.getName();
        return gname;
    }

    public void setGName(String GName) {
        this.gname = GName;
    }

    public Point getGLocation() {
        return this.getLocation();
    }

    public Dimension getGSize() {
        return this.getSize();
    }

    public List<Guava> getGComponents() {
        return new ArrayList<Guava>();
    }

    public void gBind(String table, int i) {
        DataTable dt = AppData.ds.getTable(table);
        if (!dt.rows.isEmpty()) {
            String value = "";
            Component[] comps = this.getComponents();
            for (Component comp : comps) {
                JRadioButton radioBtn = (JRadioButton) comp;
                if (radioBtn.isSelected()) {
                    value = radioBtn.getText();
                    break;
                }
            }
            dt.rows.get(i).put(this.getGName(), value);
        }
    }

    public void gBindFromDatabase(String table, int i) {
        DataTable dt = AppData.ds.getTable(table);
        if (!dt.rows.isEmpty()) {
            String value = (String) dt.rows.get(i).get(this.getGName());
            Component[] comps = this.getComponents();
            for (Component comp : comps) {
                JRadioButton radioBtn = (JRadioButton) comp;
                if (radioBtn.getText().equalsIgnoreCase(value)) {
                    radioBtn.setSelected(true);
                    break;
                }
            }
        }
    }

    public String getGDefault() {
        return this.getComponents().toString();
    }

    public String getGToolTip() {
        return this.getToolTipText();
    }

    public Domain getGDomain() {
        List<String> valueList = new ArrayList<String>();
        Component[] comps = this.getComponents();
        for (Component comp : comps) {
            JRadioButton radioBtn = (JRadioButton) comp;
            valueList.add(radioBtn.getText());
        }
        return new Enumerated(valueList);
    }

    public Guava get(String s) {
        for (Guava g : this.getGComponents())
            if (g.getGName().equals(s))
                return g;
        return null;
    }

}
